/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.business.data;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

/**
 *
 * @author alabuschagne
 */
@Entity
public class Country implements Serializable, Comparable
{

    private String code;
    private String name;
    private static final long serialVersionUID = 1L;
    private List<Province> provinces = new LinkedList<Province>();
    private boolean processed = false;

    public Country()
    {
    }

    public Country(String code, String name)
    {
        this.code = code;
        this.name = name;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (code != null ? code.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Country))
        {
            return false;
        }
        Country other = (Country) object;
        if ((this.code == null && other.code != null) || (this.code != null && (this.code.compareTo(other.code) != 0)))
        {
            return false;
        }
        return true;
    }

    /**
     * @return the provinces
     */
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "country", fetch = FetchType.LAZY)
    public List<Province> getProvinces()
    {
        return provinces;
    }

    /**
     * @param provinces the provinces to set
     */
    public void setProvinces(List<Province> provinces)
    {
        this.provinces = provinces;
    }

    @Id
    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    public boolean check(Country otherCountry)
    {
        if (otherCountry == null)
        {
            return false;
        }
        return ((code.compareTo(otherCountry.getCode()) == 0) && (name.compareTo(otherCountry.getName()) == 0));
    }

    @Override
    public Country clone()
    {
        Country newCountry = new Country();
        newCountry.setCode(code);
        newCountry.setName(name);
        if (provinces != null)
        {
            List<Province> ps = new LinkedList<Province>();
            ListIterator<Province> it = provinces.listIterator();
            while (it.hasNext())
            {
                Province p = it.next().clone();
                p.setCountry(newCountry);
                ps.add(p);
            }
            newCountry.setProvinces(ps);
        }
        return newCountry;
    }

    

    /**
     * @return the processed
     */
    @Transient
    public boolean isProcessed()
    {
        return processed;
    }

    /**
     * @param processed the processed to set
     */
    public void setProcessed(boolean processed)
    {
        this.processed = processed;
    }

    @Override
    public String toString()
    {
        return getName();
    }


    @Override
    public int compareTo(Object o)
    {
        if(o instanceof Country)
        {
            return getName().compareTo(((Country)o).getName());
        }
        return -1;
    }

    public void add(Province p)
    {
        if(provinces == null)
        {
            provinces = new LinkedList<Province>();
        }
        provinces.add(p);
    }

    public String logPrint(int level)
    {
        StringBuilder tab = new StringBuilder();
        for(int i = 0; i < level; i++)
        {
            tab.append("\t");
        }
        StringBuilder sb = new StringBuilder(tab.toString());
        sb.append("COUNTRY\n");
        sb.append(tab.toString());
        sb.append("Code: \t");
        sb.append(code);
        sb.append("\n");
        sb.append(tab.toString());
        sb.append("Name: \t");
        sb.append(name);
        return sb.toString();
    }
}
